<template>
  <div >
   <keep-alive :include="cachedViews">
      <router-view v-if="isRouterAlive"  :key="key"></router-view>
  </keep-alive>
  </div>
</template>

<script>
export default {
  props: {
    viewname: {
      type: String,
      default: 'default'
    }
  },
  data() {
    return {
      isRouterAlive: true,
      cachedViewsList: []
    }
  },
  computed: {
    cachedViews() {
      return this.$store.state.tagsView.cachedViews
    },
    key() {
      return this.$route.fullPath
    }
  },
  methods: {
    refreshFn(type) {
      // REFRESH_SELF 是更新当前router， 不打开新的router
      if (type !== 'REFRESH_SELF' && location.href.includes('from=toDo')) {
        const href = location.href.replace(/(from=)([^&]+|$)/, '$1' + 'record') // 针对流程步骤提交后刷新页面的
        this.$store.dispatch('delVisitedViews', this.$route); // 先删掉原来的菜单
        location.href = href;
        return;
      }
      if (this.cachedViewsList.indexOf(this.$route.name) > -1) {
        this.cachedViewsList.splice(this.cachedViewsList.indexOf(this.$route.name), 1);
        this.isRouterAlive = false;
        this.$nextTick(() => {
          this.isRouterAlive = true;
          this.cachedViewsList.push(this.$route.name)
        })
      }
    }
  },
  mounted() {
    this.$root.$on('refreshFn', this.refreshFn);
    this.cachedViewsList = this.$store.state.tagsView.cachedViews;
  },
  beforeDestroy() {
    this.$root.$off('refreshFn', this.refreshFn);
  }
}
</script>

<style lang="scss" scoped>
</style>
